package com.itheima.exerciseTest.method10_19.method18;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class UserDemo2 {
    public static void main(String[] args) {
        User u1 = new User("aaaaa","11111");
        User u2 = new User("bbbbb","22222");
        User u3 = new User("ccccc","33333");
        ArrayList<User> al = new ArrayList<>();
        al.add(u1);
        al.add(u2);
        al.add(u3);

        // 创建对象序列化流
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("backBurner\\e.txt"));
            oos.writeObject(al);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // 创建对象反序列化流
        ObjectInputStream ois = null;
        Object o = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("backBurner\\e.txt"));
            o = ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ArrayList<User> all = (ArrayList<User>) o;
        System.out.println(all);

    }
}
